package com.danielagarza.app;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.danielagarza.app.modelos.Evento;
import com.danielagarza.app.servicios.LeerJSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Agenda extends Fragment {

	public Agenda() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View vista = inflater.inflate(R.layout.activity_agenda, container,
				false);
		ListView lv = (ListView) vista.findViewById(R.id.listView1);

		if (isNetworkAvailable()) {
			CargarDatos t = new CargarDatos();
			t.execute();
			try {
				Gson g = new Gson();
				List<Evento> l = g.fromJson(t.get(),
						new TypeToken<List<Evento>>() {
						}.getType());
				ItemAgendaAdapter adapter = new ItemAgendaAdapter(
						this.getActivity(), l);
				lv.setAdapter(adapter);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return vista;
	}

	private class CargarDatos extends AsyncTask<Void, Integer, String> {
		@Override
		protected String doInBackground(Void... arg0) {
			String valor = LeerJSON
					.leerJSON("http://li530-140.members.linode.com:9003/servicios/agenda");
			return valor;
		}
	}

	public class ItemAgendaAdapter extends BaseAdapter {
		protected Activity activity;
		protected List<Evento> items;
		protected Typeface font;

		public ItemAgendaAdapter(Activity activity, List<Evento> items) {
			this.activity = activity;
			this.items = items;
			font = Typeface.createFromAsset(getActivity().getAssets(),
					"font2.ttf");
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View vi = convertView;

			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) activity
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				vi = inflater.inflate(R.layout.agenda_layout, null);
			}

			final Evento item = items.get(position);
			TextView nombre = (TextView) vi.findViewById(R.id.txtNombre);
			nombre.setText(item.evento);
			nombre.setTypeface(font);

			TextView cuerpo = (TextView) vi.findViewById(R.id.TextView01);
			cuerpo.setText(item.lugar);
			cuerpo.setTypeface(font);
			
			ImageButton btnImg = (ImageButton) vi.findViewById(R.id.imageButton1);
			btnImg.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(getActivity(), Mapa.class);
					List<Evento> lst = new ArrayList<Evento>();
					lst.add(item);
					intent.putExtra("puntos",new Gson().toJson(lst));
					startActivity(intent);
					
				}
			});

			/*
			 * TextView tipo = (TextView) vi.findViewById(R.id.textView2);
			 * tipo.setText(item.fecha.toString()); tipo.setTypeface(font);
			 */
			return vi;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}
	}

	public boolean isNetworkAvailable() {
		ConnectivityManager cm = (ConnectivityManager) getActivity()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}

}
